/*******************************************************************************
 * Copyright (c)  2009 Michal Sczepanski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/
package pl.vane.air.ajournal
{
	import flash.errors.IllegalOperationError;
	import flash.filesystem.File;
	
	import mx.core.mx_internal;
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.LogEventLevel;
	
	use namespace mx_internal;

	public class Journal
	{
		private var _name:String;
		
		private var _lastName:String;
		
		private var _path:String;
		
		private var _active:Boolean;
		
		private static var _journal:ILogger;
		
		mx_internal var _target:IJournalTarget;
		
		private static var instance:Journal;
		
		
		/**
		 * Constructor.
		 * 
		 * Adobe AIR Flex logging to File
		 * @param path <code>String</code>: path of the logging target.<br/>
		 * Default logging target is app-storage:/logs/ 
		 * @param name <code>String</code>: name of the file to log. Default is 'journal.Current-Date.log
		 * @param name <active>Boolean</code>: is logging active.
		 */ 
		public function Journal(path:String = null, name:String = 'journal', active:Boolean = true):void 
		{
			if (instance)
				throw new IllegalOperationError('no multiple instances on Journal');
			instance = this;

			var date:Date = new Date();
			var s:String = name+'.'+date.fullYear+'-'+date.month+'-'+date.day;
            
			_name = name;
			_active = active; 
			
			if(path)
				_path = path+s+'.log';
			else 
				_path = File.applicationStorageDirectory.nativePath+File.separator+'logs'+File.separator+s+'.log';
			initialize();
		}
		
		public static function getInstance():Journal
		{
			if (!instance)
				instance = new Journal();
			return instance;
		}
		
		/**
		 * @return path of the file currently logging to
		 */ 
		public function get path():String 
		{
			return _path;
		}
		
		/**
		 * return is logging active
		 */ 
		public function get active():Boolean 
		{
			return _active;
		}
		
		/**
		 * sets the logging active
		 */ 
		public function set active(status:Boolean):void 
		{
			_active = status;
		}
		
		/**
		 * gets the name
		 */ 
		public function get name():String 
		{
			return _name;
		}
		
		/**
		 * sets the new file name
		 */ 
		public function set name(name:String):void 
		{
			_lastName = _name;
			_name = name;
			var date:Date = new Date();
			var s:String = name+'.'+date.fullYear+'-'+date.month+'-'+date.day;
			_path = _path.substr(0,_path.lastIndexOf(File.separator))+File.separator+s+'.log';
			_target.changePath(_path);
		}
		
		public function get lastName():String 
		{
			return _lastName;
		}
		
		public function debug(message:String, ... parameters):void 
		{
			if(_active) _journal.debug(message, parameters);
		} 
		
		public function error(message:String, ... parameters):void 
		{
			if(_active) _journal.error(message, parameters);
		}
		
		public function fatal(message:String, ... parameters):void 
		{
			if(_active) _journal.fatal(message, parameters);
		}
		
		public function info(message:String, ... parameters):void 
		{
			if(_active) _journal.info(message, parameters);
		}
		
		public function warn(message:String, ... parameters):void 
		{
			if(_active) _journal.warn(message, parameters);
		}
		
		mx_internal function initialize():void
		{
			_target = new JournalTarget(_path);
			JournalTarget(_target).level = LogEventLevel.ALL;
			
			JournalTarget(_target).includeDate = JournalTarget(_target).includeTime 
			= JournalTarget(_target).includeCategory = JournalTarget(_target).includeLevel = true;
			
			Log.addTarget(_target);
			_journal = Log.getLogger(_name);
		}
		
	}
}
